package com.hk.demo.player.listpreview;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hk.demo.player.MyBaseActivity;
import com.hk.demo.player.R;
import com.hk.demo.player.http.ApiRepository;
import com.hk.demo.player.http.FirmMonitor;
import com.hk.player.core.DataCallback;
import com.hk.player.module.AutoPlayController;
import com.hk.player.module.PlayerPreviewListAdapter;
import com.zhoug.common.utils.ScreenOrientationHelper;
import com.zhoug.common.utils.ToastUtils;
import com.zhoug.zmui.dialog.list.ZMUIListDialog;
import com.zhoug.zmui.ints.OnItemClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: zhoug
 * @since: 2025-08-07
 */
public class PlayerListActivity extends MyBaseActivity {
    private static final String TAG = ">>>PlayerListActivity";
    private int mLayoutMode=LayoutMode.N1.getMode();

    private ApiRepository mApiRepository;
    private final List<FirmMonitor> mFirmMonitor = new ArrayList<>();
    private PlayerPreviewListAdapter mPlayerListAdapter;
    private ScreenOrientationHelper mScreenOrientationHelper;

    private RecyclerView mRecyclerView;
    private LinearLayout mContainerContent;
    private FrameLayout mFullscreenContainer;
    private TextView mTvLayoutMode;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_player_list;
    }

    @Override
    protected void findViews() {
        mRecyclerView = findViewById(R.id.recyclerView);
        mContainerContent = findViewById(R.id.container_content);
        mFullscreenContainer = findViewById(R.id.fullscreenContainer);
        mTvLayoutMode = findViewById(R.id.tv_layoutMode);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mTvLayoutMode.setOnClickListener(v -> {
            ZMUIListDialog<LayoutMode> dialog = new ZMUIListDialog<LayoutMode>(this) {
                @Override
                public String getText(LayoutMode itemData) {
                    return itemData.getName();
                }
            };
            dialog.setData(Arrays.asList(LayoutMode.values()));
            dialog.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    LayoutMode itemData = dialog.getItemData(position);
                    mTvLayoutMode.setText(itemData.getName());
                    switchLayoutMode(itemData.getMode());
                }
            });
            dialog.show();

        });


        mScreenOrientationHelper = new ScreenOrientationHelper(this);
        mPlayerListAdapter = new PlayerPreviewListAdapter(this, mRecyclerView, mFullscreenContainer);
        mPlayerListAdapter.setLifeCycle(getLifecycle());
        mPlayerListAdapter.setData(new ArrayList<>());
        mPlayerListAdapter.setLoadMoreEnable(true);
        mPlayerListAdapter.setOnLoadMoreListener(()->{
            loadData(2);
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        mRecyclerView.setLayoutManager(linearLayoutManager);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,1);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mPlayerListAdapter);
        mPlayerListAdapter.setOnFullscreenChangeListener(new PlayerPreviewListAdapter.OnFullscreenChangeListener() {
            @Override
            public void onEnterFullScreen() {
                mScreenOrientationHelper.changeScreenOrientation();
                mContainerContent.setVisibility(View.GONE);
            }

            @Override
            public void onExitFullscreen() {
                mScreenOrientationHelper.changeScreenOrientation();
                mContainerContent.setVisibility(View.VISIBLE);
            }
        });

        AutoPlayController autoPlayController = new AutoPlayController(mRecyclerView, linearLayoutManager);
        autoPlayController.setSwitchPlayListener(new AutoPlayController.SwitchPlayListener() {
            @Override
            public void onStartScroll() {
                mPlayerListAdapter.pausePlay();
            }

            @Override
            public void onCenterItemChange(RecyclerView.ViewHolder viewHolder) {
                if (viewHolder instanceof PlayerPreviewListAdapter.VideoViewHolder videoViewHolder) {
                    mPlayerListAdapter.startPlay(videoViewHolder);
                }
            }
        });

        loadData(1);
    }

    private void loadData(int page) {
        mApiRepository = new ApiRepository();
        mApiRepository.getPreviewMonitorList(page, 30, new DataCallback<List<FirmMonitor>>() {
            @Override
            public void onSuccess(List<FirmMonitor> firmMonitors) {
                if (firmMonitors != null && !firmMonitors.isEmpty()) {
                    mFirmMonitor.addAll(firmMonitors);
                    mPlayerListAdapter.loadSuccess(FirmMonitor.toMonitorData(firmMonitors));
                } else {
                    if(page==1){
                        ToastUtils.toastLong("没有获取到摄像头");
                    }else{
                        mPlayerListAdapter.loadMoreSuccess(null);
                    }
                }
            }

            @Override
            public void onFailure(String error) {
                ToastUtils.toastLong("获取摄像头失败:" + error);
            }
        });
    }


    /**
     * 切换网格
     * @param layoutMode
     */
    private void switchLayoutMode(int layoutMode) {
        if(mLayoutMode!=layoutMode){
            mLayoutMode=layoutMode;
            RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
            if(layoutManager instanceof GridLayoutManager gridLayoutManager){
                gridLayoutManager.setSpanCount(mLayoutMode);
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (mPlayerListAdapter.isFullScreen()) {
            mPlayerListAdapter.exitFullscreen();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mScreenOrientationHelper.onConfigurationChanged(newConfig);
    }

    public enum LayoutMode {
        N1(1, "1xN"),
        N2(2, "2xN"),
        N3(3, "3xN"),
        N4(4, "4xN");
        private final int mode;
        private final String name;

        LayoutMode(int type, String name) {
            this.mode = type;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public int getMode() {
            return mode;
        }
    }


}
